﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Com.CMS263.DAO;
using Com.CMS263.PO;
using Com.CMS263.Util;
using System.IO;
using Com.CMS263.Caches;
namespace Com.CMS263.BLL
{
    public class ArtBaseBLL
    {
        ArtBaseDAO abdao = new ArtBaseDAO();
        ExtValueDAO evdao = new ExtValueDAO();
        /// <summary>
        /// 缓存对象
        /// </summary>
        ArtCache ac = new ArtCache();
        public void Add(Com.CMS263.PO.ArtBase ab)
        {
            //保存节本内容
            abdao.Add(ab);
            IList<ExtValue> valList = ab.valueList;
            foreach (ExtValue ev in valList)
            {
                ev.baseArtId = ab.id;
                evdao.Add(ev);
            }
            //添加缓存


        }
        /// <summary>
        /// 根基条件页数返回搜索数据
        /// </summary>
        /// <param name="typeId">菜单分类编号</param>
        /// <param name="page">第几页</param>
        /// <param name="pageSize">每页几条</param>
        /// <param name="key">搜索关键词</param>
        /// <returns></returns>
        public IList<ArtBase> GetResultByKey(int menuId, int page, int pageSize, string key)
        {
            return abdao.GetResultByKey(menuId, page, pageSize, key);
        }
        public ArtBase GetArtBaseById(int id)
        {
            ArtBase ab = abdao.GetArtById(id);
            try
            {
                ab.valueList = (new ExtValueBLL()).GetExtValueByArtId(ab.id);
                return abdao.GetArtById(id);
            }
            catch (Exception)
            {
                return new ArtBase();
            }
            
        }
        public void UpdateArt(ArtBase ab)
        {
            abdao.Modify(ab);
            IList<ExtValue> valList = ab.valueList;
            if (valList != null)
            {
                foreach (ExtValue ev in valList)
                {
                    evdao.Modify(ev);
                }
            }
            string fileName = System.Configuration.ConfigurationSettings.AppSettings["cachepath"];
            fileName += "Art\\Art" + ab.id + ".txt";
            ac.AddArt(ab);
        }

        public int GetCount(int typeId, string key)
        {
            return abdao.GetResultTotalByKey(typeId, key);
        }
        /// <summary>
        /// 模糊查询
        /// </summary>
        /// <param name="typeId"></param>
        /// <param name="key"></param>
        /// <returns></returns>
        public IList<ArtBase> GetResult(int typeId, string key)
        {
            return abdao.GetResultByKey(typeId, key);
        }

        public ArtBase GetArtBaseByMenuId(int menuId)
        {
            try
            {
                return abdao.GetArtBaseListByMenuId(menuId).First();
            }
            catch (Exception)
            {

                return null;
            }
        }

        public IList<ArtBase> GetArtBaseListByMenuId(int menuId)
        {
            return abdao.GetArtBaseListByMenuId(menuId);
        }

        public void Delete(int artId)
        {
            abdao.Delete(artId);
            //删除依赖文件以更新缓存
            string fileName = System.Configuration.ConfigurationSettings.AppSettings["cachepath"];
            fileName += "Art\\Art" + artId + ".txt";


            if (File.Exists(fileName))
            {
                File.Delete(fileName);
            }
        }

        public IList<ArtBase> GetResultByMenuId(int menuId, int page, int pageSize)
        {
            return abdao.GetResultByMenuId(menuId, page, pageSize);
        }
        public int GetResultByMenuId(int menuId)
        {
            return abdao.GetResultByMenuId(menuId);
        }

        public IList<ArtBase> GetResultByMenuIdClickCount(int menuId, int page, int pageSize)
        {
            return abdao.GetResultByMenuIdClickCount(menuId, page, pageSize);
        }
        /// <summary>
        /// 获取当前的排序字段
        /// </summary>
        /// <param name="menuId"></param>
        /// <returns></returns>
        public int GetMaxArtOrderNumByMenuId(int menuId)
        {
            return abdao.GetMaxArtOrderNumByMenuId(menuId);
        }




        /// <summary>
        /// 根据关键字和menuids串查询信息
        /// </summary>
        /// <param name="menuIds"></param>
        /// <param name="key"></param>
        /// <param name="page"></param>
        /// <param name="pagesize"></param>
        /// <returns></returns>
        public IList<ArtBase> GetResultByKeyAndMenuIds(string menuIds, string key, int page, int pagesize)
        {
            return abdao.GetResultByKeyAndMenuIds(menuIds, key, page, pagesize);
        }
        /// <summary>
        /// 根据关键字和menuids串查询信息个数
        /// </summary>
        /// <param name="menuIds"></param>
        /// <param name="key"></param>
        /// <param name="page"></param>
        /// <param name="pagesize"></param>
        /// <returns></returns>
        public int GetResultByKeyAndMenuIds(string menuIds, string key)
        {
            return abdao.GetResultByKeyAndMenuIds(menuIds, key);
        }

        public IList<ArtBase> GetResultByKA(int artId, int page, int pageSize, string key,int pid)
        {
            return abdao.GetResultByKA(artId, key, page, pageSize,pid);
        }

        public int GetCountByArtId(int artId, string key,int pid)
        {
            return abdao.GetCountByArtId(artId, key,pid);
        }

        public IList<ArtBase> GetArtBaseListByArtId(int artId, string key, int page, int pageSize)
        {
            return abdao.GetArtBaseListByArtId(artId, key, page, pageSize);
        }



        ////////////////////////////////////分割线///////////////////////////////////
        public IList<ArtBase> GetListBySortId(int menuid, string sortstate, string key, int page, int pagesize)
        {
            return abdao.GetListBySortId(menuid, sortstate, key, page, pagesize);
        }
        public int GetListBySortId(int menuid, string key)
        {
            return abdao.GetListBySortId(menuid, key);
        }
        public IList<ArtBase> GetListBySortIds(string menuid, string sortstate, string key, int page, int pagesize)
        {
            return abdao.GetListBySortIds(menuid, sortstate, key, page, pagesize);
        }
        public int GetListBySortIds(string menuid, string key)
        {
            return abdao.GetListBySortIds(menuid, key);
        }
        public int GetPrev(int id, int menuid, int parentId, string sort)
        {
            return abdao.GetPrev(id, menuid, parentId, sort);
        }
    }
}
